home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / Table / Sources / Content.cpp < prev    next >
Encoding:
Text File  |  1996-08-16  |  15.5 KB  |  516 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Content.cpp
  4. //    Release Version:    $ ODF 1 $ 
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #ifndef CONTENT_H
  11. #include "Content.h"
  12. #endif
  13.  
  14. #ifndef PART_H
  15. #include "Part.h"
  16. #endif
  17.  
  18. #ifndef FRAME_H
  19. #include "Frame.h"
  20. #endif
  21.  
  22. #ifndef PROXY_H
  23. #include "Proxy.h"
  24. #endif
  25.  
  26. #ifndef SELECTION_H
  27. #include "Selection.h"
  28. #endif
  29.  
  30. #ifndef LINKING_H
  31. #include "Linking.h"
  32. #endif
  33.  
  34. // ----- Framework Includes -----
  35.  
  36. #ifndef FWFRMING_H
  37. #include "FWFrming.h"
  38. #endif
  39.  
  40. #ifndef FWPRESEN_H
  41. #include "FWPresen.h"
  42. #endif
  43.  
  44. //========================================================================================
  45. //    Runtime Info
  46. //========================================================================================
  47.  
  48. #ifdef FW_BUILD_MAC
  49. #pragma segment odfTable
  50. #endif
  51.  
  52. FW_DEFINE_AUTO(CTableContent)
  53. FW_DEFINE_AUTO(CTableSelectionContent)
  54.  
  55. //========================================================================================
  56. //    class CTableContent
  57. //========================================================================================
  58.  
  59. //----------------------------------------------------------------------------------------
  60. //    CTableContent::CTableContent
  61. //----------------------------------------------------------------------------------------
  62.  
  63. CTableContent::CTableContent(Environment* ev, CTablePart* part) :
  64.     FW_CEmbeddingContent(ev, part),
  65.     fTablePart(part),
  66.     fProxys(NULL)
  67. {
  68.     // ----- Create list of proxies -----
  69.     fProxys = FW_NEW(CTableProxyCollection, ());
  70.  
  71.     fCells.fX = kMaxCols;
  72.     fCells.fY = kMaxRows;
  73.     
  74.     // ----- Initialize arrays
  75.     unsigned long s;
  76.     for(s=0; s < kMaxCols; s++)
  77.         fWidth[s] = kDefaultCellWidth;
  78.     for(s=0; s < kMaxRows; s++)
  79.         fHeight[s] = kDefaultCellHeight;
  80. }
  81.  
  82. //----------------------------------------------------------------------------------------
  83. //    CTableContent::~CTableContent
  84. //----------------------------------------------------------------------------------------
  85.  
  86. CTableContent::~CTableContent()
  87. {
  88.     // just delete all the CTableProxy
  89.     
  90.     if (fProxys)
  91.     {
  92.         CTableProxy* proxy;
  93.         while ((proxy = (CTableProxy*)fProxys->First()) != NULL)
  94.         {
  95.             fProxys->Remove(proxy);
  96.             delete proxy;
  97.         }        
  98.     }
  99.         
  100.     delete fProxys;
  101. }
  102.  
  103. //----------------------------------------------------------------------------------------
  104. //    CTableContent::Externalize
  105. //----------------------------------------------------------------------------------------
  106.  
  107. void CTableContent::Externalize(Environment* ev,
  108.                                 ODStorageUnit* storageUnit, 
  109.                                 FW_EStorageKinds storageKind,
  110.                                 FW_CCloneInfo* cloneInfo)
  111. {
  112.     FW_UNUSED(storageKind);
  113.  
  114.     // ---- Write out grid info ----
  115.     FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, fTablePart->GetPartKind(ev));
  116.     FW_CWritableStream archive(suSink);
  117.  
  118.     archive << fCells.fX;
  119.     archive << fCells.fY;
  120.     archive.Write(fWidth, fCells.fX);
  121.     archive.Write(fHeight, fCells.fY);
  122.  
  123.     // ---- Write number of embedded parts ----
  124.     unsigned long partCount = fProxys->Count();
  125.     archive << partCount;
  126.  
  127.     // ---- Write out embedded frames ----
  128.     if (partCount > 0)
  129.     {
  130.         CTableProxyCollectionIterator iter(fProxys);
  131.         for (CTableProxy* proxy = iter.First(); iter.IsNotComplete(); proxy = iter.Next())
  132.         {
  133.             CCell cell = proxy->GetCell();
  134.             archive << cell.fX;
  135.             archive << cell.fY;
  136.             proxy->Externalize(ev, suSink->GetStorageUnitView(ev), cloneInfo);
  137.         }
  138.     }
  139.  
  140.     //--- Write source and destination links, if any ---
  141.     CTableLinkManager* linkMgr = (CTableLinkManager*) fTablePart->GetLinkManager(ev);
  142.     linkMgr->ExternalizeLinks(ev, storageUnit, cloneInfo);
  143. }
  144.  
  145. //----------------------------------------------------------------------------------------
  146. //    CTableContent::Internalize
  147. //----------------------------------------------------------------------------------------
  148.  
  149. FW_Boolean CTableContent::Internalize(Environment* ev,
  150.                                     ODStorageUnit* storageUnit, 
  151.                                     FW_EStorageKinds storageKind,
  152.                                     FW_CCloneInfo* cloneInfo)
  153. {
  154.     FW_UNUSED(storageKind);
  155.  
  156.     if (!storageUnit->Exists(ev, kODPropContents, fTablePart->GetPartKind(ev), 0))
  157.         return false;
  158.  
  159.     // ---- Read grid info -----
  160.     FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, fTablePart->GetPartKind(ev));
  161.     FW_PBufferedSink sink(ev, suSink);
  162.     FW_CReadableStream archive(sink);
  163.  
  164.     archive >> fCells.fX;
  165.     archive >> fCells.fY;
  166.  
  167.     FW_ASSERT(fCells.fX == kMaxCols);
  168.     archive.Read(fWidth, fCells.fX);
  169.  
  170.     FW_ASSERT(fCells.fY == kMaxRows);
  171.     archive.Read(fHeight, fCells.fY);
  172.  
  173.     // ---- Read number of embedded parts ----
  174.     unsigned long partCount;
  175.     archive >> partCount;
  176.  
  177.     // ---- Read embedded parts ----
  178.     CCell cell;
  179.     CTableProxy* proxy;
  180.     for (unsigned long i = 0; i < partCount; i++)
  181.     {
  182.         archive >> cell.fX;
  183.         archive >> cell.fY;
  184.         proxy = new CTableProxy(ev, fTablePart, this, fTablePart->GetTablePresentation(ev));
  185.         proxy->SetCell(cell);
  186.         proxy->Internalize(ev, suSink->GetStorageUnitView(ev), cloneInfo);        // read part and embed it
  187.         this->AddProxy(proxy);
  188.     }
  189.  
  190.     // ----- Read link information -----
  191.     CTableLinkManager* linkMgr = (CTableLinkManager*) fTablePart->GetLinkManager(ev);
  192.     linkMgr->InternalizeLinks(ev, storageUnit);
  193.     
  194.     return true;
  195. }
  196.  
  197. //----------------------------------------------------------------------------------------
  198. //    CTableContent::IsDataOnlyOneProxy
  199. //----------------------------------------------------------------------------------------
  200.  
  201. FW_MProxy* CTableContent::IsDataOnlyOneProxy(Environment* ev) const
  202. {
  203.     // This method is only called on the content object associated with the selection to
  204.     // test for the Single Embedded Frame case. The part content should just retur NULL.
  205.     
  206.     return NULL;
  207. }
  208.  
  209. //----------------------------------------------------------------------------------------
  210. //    CTableContent::CellToProxy
  211. //----------------------------------------------------------------------------------------
  212.  
  213. CTableProxy* CTableContent::CellToProxy(const CCell& cell) const
  214. {
  215.     // Convert a cell to a Proxy pointer by going through the 
  216.     
  217.     CTableProxyCollectionIterator iter(fProxys);
  218.     for (CTableProxy* proxy = iter.First(); iter.IsNotComplete(); proxy = iter.Next())
  219.     {
  220.         if (proxy->GetCell() == cell)
  221.             return proxy;
  222.     }
  223.  
  224.     return NULL;
  225. }
  226.  
  227. //----------------------------------------------------------------------------------------
  228. //    CTableContent::GetWidth
  229. //----------------------------------------------------------------------------------------
  230.  
  231. FW_Fixed CTableContent::GetWidth(short c) const
  232. {
  233.     return (c < fCells.fX ? fWidth[c] : kDefaultCellWidth);
  234. }
  235.  
  236. //----------------------------------------------------------------------------------------
  237. //    CTableContent::GetHeight
  238. //----------------------------------------------------------------------------------------
  239.  
  240. FW_Fixed CTableContent::GetHeight(short r) const
  241. {
  242.     return (r < fCells.fY ? fHeight[r] : kDefaultCellHeight);
  243. }
  244.  
  245. //----------------------------------------------------------------------------------------
  246. //    CTableContent::SetWidth
  247. //----------------------------------------------------------------------------------------
  248.  
  249. void CTableContent::SetWidth(short c, FW_Fixed w)
  250. {
  251.     FW_ASSERT(c < fCells.fX);
  252.     fWidth[c] = w;
  253. }
  254.  
  255. //----------------------------------------------------------------------------------------
  256. //    CTableContent::SetHeight
  257. //----------------------------------------------------------------------------------------
  258.  
  259. void CTableContent::SetHeight(short r, FW_Fixed h)
  260. {
  261.     FW_ASSERT(r < fCells.fY);
  262.     fHeight[r] = h;
  263. }
  264.  
  265. //----------------------------------------------------------------------------------------
  266. //    CTableContent::AddProxy
  267. //----------------------------------------------------------------------------------------
  268.  
  269. void CTableContent::AddProxy(CTableProxy* proxy)
  270. {
  271.     fProxys->AddLast(proxy);
  272. }
  273.  
  274. //----------------------------------------------------------------------------------------
  275. //    CTableContent::RemoveProxy
  276. //----------------------------------------------------------------------------------------
  277.  
  278. void CTableContent::RemoveProxy(CTableProxy* proxy)
  279. {
  280.     fProxys->Remove(proxy);
  281. }
  282.     
  283. //----------------------------------------------------------------------------------------
  284. //    CTableContent::FindLeft
  285. //----------------------------------------------------------------------------------------
  286.  
  287. FW_Fixed CTableContent::FindLeft(short col) const
  288. {
  289.     FW_Fixed x = kHMargin + kBorderWidth;
  290.     for(short c = 0; c < col; c += 1)
  291.         x += this->GetWidth(c) + kBorderWidth;
  292.     return x;
  293. }
  294.  
  295. //----------------------------------------------------------------------------------------
  296. //    CTableContent::FindTop
  297. //----------------------------------------------------------------------------------------
  298.  
  299. FW_Fixed CTableContent::FindTop(short row) const
  300. {
  301.     FW_Fixed y = kVMargin + kBorderHeight;
  302.     for(short r = 0; r < row; r += 1)
  303.         y += this->GetHeight(r) + kBorderWidth;
  304.     return y;
  305. }
  306.  
  307. //----------------------------------------------------------------------------------------
  308. //    CTableContent::FindRect
  309. //----------------------------------------------------------------------------------------
  310.  
  311. void CTableContent::FindRect(const CCell& cell, FW_CRect& rect) const
  312. {
  313.     FW_Fixed x = this->FindLeft(cell.fX);
  314.     FW_Fixed y = this->FindTop (cell.fY);
  315.  
  316.     rect.Set(x, y,
  317.              x + this->GetWidth(cell.fX),
  318.              y + this->GetHeight(cell.fY));
  319. }
  320.  
  321. //----------------------------------------------------------------------------------------
  322. //    CTableContent::Resize
  323. //----------------------------------------------------------------------------------------
  324.  
  325. void CTableContent::Resize(Environment* ev, const CCell& cell, ETableLoc tl, const FW_CPoint& delta)
  326. {
  327.     CCell topLeftCell(cell);
  328.     
  329.     if (delta.x != FW_kFixed0)
  330.     {
  331.         if ((tl & kTLLeftBorder) != 0)
  332.             topLeftCell.fX -= 1;
  333.         SetWidth(topLeftCell.fX, GetWidth (topLeftCell.fX) + delta.x);        
  334.     }
  335.     
  336.     if (delta.y != FW_kFixed0)
  337.     {
  338.         if ((tl & kTLTopBorder) != 0)
  339.             topLeftCell.fY -= 1;
  340.         SetHeight(topLeftCell.fY, GetHeight(topLeftCell.fY) + delta.y);
  341.     }
  342.     
  343.     // Move all embedded frames
  344.     CTableProxyCollectionIterator ite(fProxys);
  345.     for (CTableProxy* proxy = ite.First(); ite.IsNotComplete(); proxy = ite.Next())
  346.     {
  347.         // Get operations being performed
  348.         CCell frCell = proxy->GetCell();
  349.         FW_Boolean resizing = (frCell.fX == topLeftCell.fX || frCell.fY == topLeftCell.fY);
  350.         FW_Boolean moving   = (frCell.fX > topLeftCell.fX || frCell.fY > topLeftCell.fY);
  351.         
  352.         if (resizing || moving)
  353.             proxy->MoveEmbeddedFrames(ev, frCell);
  354.     }
  355.  
  356.     fTablePart->Changed(ev);
  357. }
  358.  
  359. //========================================================================================
  360. //    class CTableSelectionContent
  361. //========================================================================================
  362.  
  363. //----------------------------------------------------------------------------------------
  364. //    CTableSelectionContent::CTableSelectionContent
  365. //----------------------------------------------------------------------------------------
  366.  
  367. CTableSelectionContent::CTableSelectionContent(Environment* ev, CTablePart* part, CTableContent* content) :
  368.     FW_CEmbeddingContent(ev, part),
  369.     fTableContent(content),
  370.     fTablePart(part),
  371.     fCell(0, 0)
  372. {
  373. }
  374.  
  375. //----------------------------------------------------------------------------------------
  376. //    CTableSelectionContent::~CTableSelectionContent
  377. //----------------------------------------------------------------------------------------
  378.  
  379. CTableSelectionContent::~CTableSelectionContent()
  380. {
  381. }
  382.  
  383.  
  384. //----------------------------------------------------------------------------------------
  385. //    IsDataOnlyOneProxy
  386. //----------------------------------------------------------------------------------------
  387.  
  388. FW_MProxy* CTableSelectionContent::IsDataOnlyOneProxy(Environment* ev) const
  389. {
  390.     return fTableContent->CellToProxy(fCell);
  391. }
  392.  
  393. //----------------------------------------------------------------------------------------
  394. //    CTableSelectionContent::Internalize
  395. //----------------------------------------------------------------------------------------
  396.  
  397. FW_Boolean CTableSelectionContent::Internalize(Environment* ev,
  398.                                             ODStorageUnit* storageUnit, 
  399.                                             FW_EStorageKinds storageKind,
  400.                                             FW_CCloneInfo* cloneInfo)
  401. {
  402.     // Should never be called because I always internalize a single embedded frame
  403.     FW_UNUSED(storageUnit);
  404.     FW_UNUSED(storageKind);
  405.     FW_UNUSED(cloneInfo);
  406.  
  407.     return false;
  408. }
  409.  
  410. //----------------------------------------------------------------------------------------
  411. //    CTableSelectionContent::Externalize
  412. //----------------------------------------------------------------------------------------
  413.  
  414. void CTableSelectionContent::Externalize(Environment* ev,
  415.                                          ODStorageUnit* storageUnit, 
  416.                                          FW_EStorageKinds storageKind,
  417.                                          FW_CCloneInfo* cloneInfo)
  418. {
  419.     FW_UNUSED(storageKind);
  420.  
  421.     CTableProxy* proxy = GetSelectedProxy(ev);
  422.     if (proxy)
  423.     {
  424.         FW_PStorageUnitSink suSink(ev, storageUnit, kODPropContents, fTablePart->GetPartKind(ev));
  425.         proxy->Externalize(ev, suSink->GetStorageUnitView(ev), cloneInfo);
  426.     }
  427. }
  428.  
  429. //----------------------------------------------------------------------------------------
  430. //    CTableSelectionContent::GetSelectedProxy
  431. //----------------------------------------------------------------------------------------
  432.  
  433. CTableProxy* CTableSelectionContent::GetSelectedProxy(Environment* ev) const
  434. {
  435.     return fTableContent->CellToProxy(fCell);
  436. }
  437.  
  438. //----------------------------------------------------------------------------------------
  439. //    CTableSelectionContent::SingleEmbeddedFrameInternalized
  440. //----------------------------------------------------------------------------------------
  441.  
  442. void CTableSelectionContent::SingleEmbeddedFrameInternalized(Environment* ev, 
  443.                                                           FW_CEmbeddingFrame* scopeFrame,
  444.                                                           ODPart* embeddedPart, 
  445.                                                           ODFrame* embeddedFrame,
  446.                                                           ODShape* suggestedShape,
  447.                                                           ODTypeToken viewType)
  448. {
  449.     // A single embedded frame has been internalized (Drag&Drop, Clipboard or Insert).
  450.     
  451.     FW_UNUSED(suggestedShape);
  452.     
  453.     CCell selectionCell = GetCell();
  454.     
  455.     CTableProxy* proxy = fTableContent->CellToProxy(selectionCell);
  456.     
  457.     FW_CRect rect;
  458.     fTableContent->FindRect(selectionCell, rect);
  459.     rect.Place(FW_kZeroPoint);
  460.  
  461.     FW_CAcquiredODShape aqShape = ::FW_NewODShape(ev, rect);
  462.  
  463.     // ----- Create the proxy -----
  464.     CTableProxy* newProxy = new CTableProxy(ev, fTablePart, fTableContent, scopeFrame->GetPresentation(ev));
  465.     newProxy->SetCell(selectionCell);
  466.  
  467.     // ----- Add the newProxy to the part -----
  468.     fTableContent->AddProxy(newProxy);
  469.     
  470.     // ----- Embed the part or the frame -----
  471.     scopeFrame->GetPresentation(ev)->Embed(ev, 
  472.                                             embeddedPart,
  473.                                             embeddedFrame,
  474.                                             kODFrameObject,    
  475.                                             newProxy,
  476.                                             aqShape,
  477.                                               viewType,
  478.                                               NULL,        // no presentation
  479.                                               0,            // group id
  480.                                               false,        // overlaid
  481.                                               false);        // sub frame
  482.  
  483.     // ----- Delete the old proxy if there was one in the cell
  484.     if (proxy != NULL)
  485.     {
  486.         proxy->DetachEmbeddedFrames(ev);
  487.         fTableContent->RemoveProxy(proxy);
  488.         delete proxy;
  489.     }
  490.  
  491.     // ----- Select the new proxy -----
  492.     newProxy->Selected(TRUE);
  493.     
  494.     // ----- Change its hilite state
  495.     if (scopeFrame && scopeFrame->HasSelectionFocus(ev))
  496.         newProxy->ChangeHighlight(ev, scopeFrame->GetWindow(ev)->IsActive(ev) ? kODFullHighlight : kODDimHighlight, scopeFrame);
  497.  
  498. }
  499.  
  500. //----------------------------------------------------------------------------------------
  501. //    CTableSelectionContent::CreateDataFrameShape
  502. //----------------------------------------------------------------------------------------
  503.  
  504. ODShape* CTableSelectionContent::CreateDataFrameShape(Environment* ev) const
  505. {
  506.     // return the shape used for the kODPropFrameShape property.
  507.     
  508.     FW_CRect rect;
  509.     fTableContent->FindRect(GetCell(), rect);
  510.  
  511.     ODShape* shape = ::FW_NewODShape(ev, rect);
  512.     return shape;
  513. }
  514.  
  515.  
  516.